/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.fenType.service;

import com.fzhucc.modules.fenType.domain.FenType;
import com.fzhucc.modules.sciEnterprise.domain.SciEnterprise;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.fenType.repository.FenTypeRepository;
import com.fzhucc.modules.fenType.domain.dto.FenTypeDto;
import com.fzhucc.modules.fenType.domain.dto.FenTypeQueryCriteria;
import com.fzhucc.modules.fenType.service.mapstruct.FenTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2024-06-05
**/
@Service
@RequiredArgsConstructor
public class FenTypeService {

    private final FenTypeRepository fenTypeRepository;
    private final FenTypeMapper fenTypeMapper;

    public Map<String,Object> queryAll(FenTypeQueryCriteria criteria, Pageable pageable){
        criteria.setIsDelete(0);
        Page<FenType> page = fenTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(fenTypeMapper::toDto));
    }

    public List<FenTypeDto> queryAll(FenTypeQueryCriteria criteria){
        criteria.setIsDelete(0);
        return fenTypeMapper.toDto(fenTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public FenTypeDto findById(Integer id) {
        FenType fenType = fenTypeRepository.findById(id).orElseGet(FenType::new);
        ValidationUtil.isNull(fenType.getId(),"FenType","id",id);
        return fenTypeMapper.toDto(fenType);
    }

    @Transactional(rollbackFor = Exception.class)
    public FenTypeDto create(FenType resources) {
        resources.setIsDelete(0);
        return fenTypeMapper.toDto(fenTypeRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(FenType resources) {
        FenType fenType = fenTypeRepository.findById(resources.getId()).orElseGet(FenType::new);
        ValidationUtil.isNull( fenType.getId(),"FenType","id",resources.getId());
        fenType.copy(resources);
        fenTypeRepository.save(fenType);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            //fenTypeRepository.deleteById(id);

            Optional<FenType> res = fenTypeRepository.findById(id);
            if (res.isPresent()) {
                FenType fenType = res.get();
                fenType.setIsDelete(1);
                fenType.setIsUse(0);
                fenTypeRepository.save(fenType);
            }
        }
    }

    public void download(List<FenTypeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FenTypeDto fenType : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("分类", fenType.getType());
            map.put("对应访问的分数", fenType.getPoint());
            map.put("是否使用", fenType.getIsUse());
            map.put("是否假删除 删除后设置为不可使用", fenType.getIsDelete());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}